package com.light.gulimall.product.service.impl;

import com.baomidou.mybatisplus.extension.conditions.update.LambdaUpdateChainWrapper;
import com.light.gulimall.product.dto.AttrDTO;
import com.light.gulimall.product.entity.AttrGroupEntity;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.function.Consumer;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.light.common.utils.PageUtils;
import com.light.common.utils.Query;

import com.light.gulimall.product.dao.AttrAttrgroupRelationDao;
import com.light.gulimall.product.entity.AttrAttrgroupRelationEntity;
import com.light.gulimall.product.service.AttrAttrgroupRelationService;
import org.springframework.transaction.annotation.Transactional;


@Service("attrAttrgroupRelationService")
public class AttrAttrgroupRelationServiceImpl extends ServiceImpl<AttrAttrgroupRelationDao, AttrAttrgroupRelationEntity> implements AttrAttrgroupRelationService {

    @Autowired
    private AttrAttrgroupRelationDao attrAttrgroupRelationDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrAttrgroupRelationEntity> page = this.page(
                new Query<AttrAttrgroupRelationEntity>().getPage(params),
                new QueryWrapper<AttrAttrgroupRelationEntity>()
        );

        return new PageUtils(page);
    }

    /**
     * 添加属性与分组关联关系
     *
     * @param dto [{
     *            "attrGroupId": 0, //分组id
     *            "attrId": 0, //属性id
     *            }]
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveRelation(List<AttrDTO> dto) {
        dto.forEach(attrDTO -> {
            this.attrAttrgroupRelationDao.updateByAttrId(attrDTO);
        });

    }


    /**
     * 删除属性与分组的关联关系
     *
     * @param dto [{"attrId":1,"attrGroupId":2}]
     */
    @Override
    public void deleteRelation(List<AttrDTO> dto) {
        ArrayList<AttrAttrgroupRelationEntity> attrGroupEntities = this.getSaveOrDeleteData(dto);
        List<Long> attrIds = attrGroupEntities.stream().map(AttrAttrgroupRelationEntity::getAttrId).collect(Collectors.toList());
        QueryWrapper<AttrAttrgroupRelationEntity> attrAttrgroupRelationEntityQueryWrapper = new QueryWrapper<>();
        attrAttrgroupRelationEntityQueryWrapper.in("attr_id",attrIds);
        this.remove(attrAttrgroupRelationEntityQueryWrapper);
    }

    private ArrayList<AttrAttrgroupRelationEntity> getSaveOrDeleteData(List<AttrDTO> dto) {
        ArrayList<AttrAttrgroupRelationEntity> attrGroupEntities = new ArrayList<>();
        for (AttrDTO attrDTO : dto) {
            AttrAttrgroupRelationEntity attrAttrgroupRelationEntity = new AttrAttrgroupRelationEntity();
            BeanUtils.copyProperties(attrDTO,attrAttrgroupRelationEntity);
            attrGroupEntities.add(attrAttrgroupRelationEntity);
        }
        return attrGroupEntities;
    }

}